package com.bytecodezz.app.framework.base.binding

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.bytecodezz.app.framework.base.general.BaseFragment

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/7/5 16:52
 * @package: com.bytecodezz.app.framework.base.binding
 *
 * @description: 支持ViewBinding的Fragment基类
 * @use: 覆盖 onCreateViewBinding 方法，返回对应 ViewBinding
 * 如果不需要(建议不需要的时候不要使用该类继承) ViewBinding 功能,实现 generateContentView(): Int? 返回对应布局ID即可
 */
abstract class BaseViewFragment<B : ViewBinding> : BaseFragment() {
    private var bindingParams: Triple<LayoutInflater, ViewGroup?, Bundle?>? = null
    protected val binding by lazy {
        onCreateViewBinding(
            bindingParams!!.first,
            bindingParams!!.second,
            bindingParams!!.third
        ).also {
            bindingParams = null
        }
    }

    /**
     * 只有当 generateContentView(): Int? 返回为null的时候才会走该方法
     */
    override fun generateContentView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        bindingParams = Triple(inflater, container, savedInstanceState)
        return binding.root
    }

    /**
     * 如果需要使用ViewBinding功能，请不要重写该方法，需要重写 onCreateViewBinding方法并返回对应的ViewBinding
     * 如果不需要使用ViewBinding功能，请重写该方法，并返回布局id
     */
    override fun generateContentView(): Int? {
        return null
    }

    /**
     * 当需要使用ViewBinding功能的时候，只重写该方法即可，返回示例如下：
     * FragmentTestBinding.inflate(inflater, container, false)
     */
    protected abstract fun onCreateViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): B
}